<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>example/c/busparams_tq.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('example_2c_2busparams_tq_8c-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">example/c/busparams_tq.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** Set bus parameters using setBusParamsTq and setBusParamsFdTq read them back</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="canlib_8h.html">canlib.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a name="a0"></a><a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf[50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <a name="a1"></a><a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat = -1;</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hndCan, hndCanFd;</div><div class="line"></div><div class="line">  <span class="comment">// Structs for bus parameters</span></div><div class="line">  <a name="_a2"></a><a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> busParams = {8, 2, 2, 1, 3, 20};</div><div class="line">  <a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> busParamsReturn;</div><div class="line"></div><div class="line">  <a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> arbitration = {40, 8, 8, 8, 23, 2};</div><div class="line">  <a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> data = {20, 15, 4, 4, 0, 2};</div><div class="line">  <a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> arbitrationReturn;</div><div class="line">  <a class="code" href="structkv_bus_params_tq.html">kvBusParamsTq</a> dataReturn;</div><div class="line"></div><div class="line">  <a name="a3"></a><a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  <span class="comment">// Open first channel in classic CAN mode</span></div><div class="line">  hndCan = <a name="a4"></a><a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(0, 0);</div><div class="line">  <span class="keywordflow">if</span> ((<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hndCan &lt; 0) {</div><div class="line">      printf(<span class="stringliteral">&quot;canOpenChannel in classic CAN mode failed with status: (%d)\n&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hndCan);</div><div class="line">      <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Set bus parameters</span></div><div class="line">  stat = <a name="a5"></a><a class="code" href="group___c_a_n.html#ga50f169c6f9dd6df93b17c322e8e4fd00">canSetBusParamsTq</a>(hndCan, busParams);</div><div class="line">  check(<span class="stringliteral">&quot;canSetBusParamsTq&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">// Open second channel in CAN FD mode</span></div><div class="line">  hndCanFd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(1, <a name="a6"></a><a class="code" href="canlib_8h.html#a10aad075bfb9891aa98cf3c131977dc3">canOPEN_CAN_FD</a>);</div><div class="line">  <span class="keywordflow">if</span> ((<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hndCanFd &lt; 0) {</div><div class="line">      printf(<span class="stringliteral">&quot;canOpenChannel in CAN FD mode failed with status: (%d)\n&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hndCanFd);</div><div class="line">      <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Set bus parameters</span></div><div class="line">  stat = <a name="a7"></a><a class="code" href="group___c_a_n.html#ga4beeb28ede2e21a2988076e66f873723">canSetBusParamsFdTq</a>(hndCanFd, arbitration, data);</div><div class="line">  check(<span class="stringliteral">&quot;canSetBusParamsTq&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">// Read classic CAN bus parameters back</span></div><div class="line">  stat = <a name="a8"></a><a class="code" href="group___c_a_n.html#gae517a9c243c446e040f6f8898d09a677">canGetBusParamsTq</a>(hndCan, &amp;busParamsReturn);</div><div class="line">  check(<span class="stringliteral">&quot;canGetBusParamsTq&quot;</span>, stat);</div><div class="line">  printf(<span class="stringliteral">&quot;\nClassic CAN: tq %u, phase1 %u, phase2 %u, sjw %u, prop %u, prescaler %u\n&quot;</span>,</div><div class="line">          busParamsReturn.<a name="a9"></a><a class="code" href="structkv_bus_params_tq.html#a4559f9255ccc9f785a33412fe5496d1a">tq</a>, busParamsReturn.<a name="a10"></a><a class="code" href="structkv_bus_params_tq.html#aa8c2aacf694615ddaff84e31b31ae0ff">phase1</a>, busParamsReturn.<a name="a11"></a><a class="code" href="structkv_bus_params_tq.html#a87167f9802ef5563e4236d5710e1b65b">phase2</a>,</div><div class="line">          busParamsReturn.<a name="a12"></a><a class="code" href="structkv_bus_params_tq.html#a3b769dc9a0e203dea3cc931bdb304377">sjw</a>, busParamsReturn.<a name="a13"></a><a class="code" href="structkv_bus_params_tq.html#a4a8f6c91eefb9c6bf448592aac44153d">prop</a>, busParamsReturn.<a name="a14"></a><a class="code" href="structkv_bus_params_tq.html#af263c600d546b48e74f8f7ac7a891533">prescaler</a>);</div><div class="line"></div><div class="line">  <span class="comment">// Read CAN FD bus parameters back</span></div><div class="line">  stat = <a name="a15"></a><a class="code" href="group___c_a_n.html#ga2ca01a0c71b8c3b5204f07b051eb4758">canGetBusParamsFdTq</a>(hndCanFd, &amp;arbitrationReturn, &amp;dataReturn);</div><div class="line">  check(<span class="stringliteral">&quot;canGetBusParamsFdTq&quot;</span>, stat);</div><div class="line">  printf(<span class="stringliteral">&quot;\nArbitration phase: tq %u, phase1 %u, phase2 %u, sjw %u, prop %u, prescaler %u\n&quot;</span>,</div><div class="line">          arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#a4559f9255ccc9f785a33412fe5496d1a">tq</a>, arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#aa8c2aacf694615ddaff84e31b31ae0ff">phase1</a>, arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#a87167f9802ef5563e4236d5710e1b65b">phase2</a>,</div><div class="line">          arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#a3b769dc9a0e203dea3cc931bdb304377">sjw</a>, arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#a4a8f6c91eefb9c6bf448592aac44153d">prop</a>, arbitrationReturn.<a class="code" href="structkv_bus_params_tq.html#af263c600d546b48e74f8f7ac7a891533">prescaler</a>);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\nData phase: tq %u, phase1 %u, phase2 %u, sjw %u, prop %u, prescaler %u\n&quot;</span>,</div><div class="line">          dataReturn.<a class="code" href="structkv_bus_params_tq.html#a4559f9255ccc9f785a33412fe5496d1a">tq</a>, dataReturn.<a class="code" href="structkv_bus_params_tq.html#aa8c2aacf694615ddaff84e31b31ae0ff">phase1</a>, dataReturn.<a class="code" href="structkv_bus_params_tq.html#a87167f9802ef5563e4236d5710e1b65b">phase2</a>,</div><div class="line">          dataReturn.<a class="code" href="structkv_bus_params_tq.html#a3b769dc9a0e203dea3cc931bdb304377">sjw</a>, dataReturn.<a class="code" href="structkv_bus_params_tq.html#a4a8f6c91eefb9c6bf448592aac44153d">prop</a>, dataReturn.<a class="code" href="structkv_bus_params_tq.html#af263c600d546b48e74f8f7ac7a891533">prescaler</a>);</div><div class="line"></div><div class="line">  <a name="a16"></a><a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hndCan);</div><div class="line">  <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hndCanFd);</div><div class="line"></div><div class="line">  stat = <a name="a17"></a><a class="code" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary</a>();</div><div class="line">  check(<span class="stringliteral">&quot;canUnloadLibrary&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
